package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.useroldman;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.useroldman.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.useroldman.UserOldmanDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.useroldman.UserOldmanConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.useroldman.UserOldmanService;

@Tag(name = "管理后台 - 用户关联老人")
@RestController
@RequestMapping("/fengdong.smartelderly/user-oldman")
@Validated
public class UserOldmanController {

    @Resource
    private UserOldmanService userOldmanService;

    @PostMapping("/create")
    @Operation(summary = "创建用户关联老人")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:create')")
    public CommonResult<Long> createUserOldman(@Valid @RequestBody UserOldmanCreateReqVO createReqVO) {
        return success(userOldmanService.createUserOldman(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户关联老人")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:update')")
    public CommonResult<Boolean> updateUserOldman(@Valid @RequestBody UserOldmanUpdateReqVO updateReqVO) {
        userOldmanService.updateUserOldman(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关联老人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:delete')")
    public CommonResult<Boolean> deleteUserOldman(@RequestParam("id") Long id) {
        userOldmanService.deleteUserOldman(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关联老人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:query')")
    public CommonResult<UserOldmanRespVO> getUserOldman(@RequestParam("id") Long id) {
        UserOldmanDO userOldman = userOldmanService.getUserOldman(id);
        return success(UserOldmanConvert.INSTANCE.convert(userOldman));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户关联老人列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:query')")
    public CommonResult<List<UserOldmanRespVO>> getUserOldmanList(@RequestParam("ids") Collection<Long> ids) {
        List<UserOldmanDO> list = userOldmanService.getUserOldmanList(ids);
        return success(UserOldmanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户关联老人分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:query')")
    public CommonResult<PageResult<UserOldmanRespVO>> getUserOldmanPage(@Valid UserOldmanPageReqVO pageVO) {
        PageResult<UserOldmanDO> pageResult = userOldmanService.getUserOldmanPage(pageVO);
        return success(UserOldmanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户关联老人 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:user-oldman:export')")
    @OperateLog(type = EXPORT)
    public void exportUserOldmanExcel(@Valid UserOldmanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserOldmanDO> list = userOldmanService.getUserOldmanList(exportReqVO);
        // 导出 Excel
        List<UserOldmanExcelVO> datas = UserOldmanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户关联老人.xls", "数据", UserOldmanExcelVO.class, datas);
    }

}
